package cn.macswelle.campusecurity.raspberry1.service;

import cn.macswelle.campusecurity.common.dto.requestDto.RecordDto;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.Pin;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.util.CommandArgumentParser;
import org.springframework.stereotype.Component;

@Component
public class CODetection extends Pi4jUtil {

  private final Pin pin = CommandArgumentParser.getPin(RaspiPin.class, RaspiPin.GPIO_29);
  public final GpioPinDigitalInput input = gpio.provisionDigitalInputPin(pin);

  @Override
  public void execute() {
    if (input.isLow()) {
      logger.info("检测到一氧化碳");
      RecordDto recordDto = new RecordDto();
      recordDto.setCreateTime(System.currentTimeMillis());
      recordDto.setEvent("检测到一氧化碳");
      recordDto.setLocation(deviceInfo.location);
      amqpService.sendRecord(true, recordDto);
    }
    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  @Override
  public boolean preExecute() {
    return true;
  }

  @Override
  public void postExecute() {

  }
}
